package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TDaRealtimeAllBo;
import org.dromara.system.domain.vo.TDaRealtimeAllVo;
import org.dromara.system.domain.TDaRealtimeAll;
import org.dromara.system.mapper.TDaRealtimeAllMapper;
import org.dromara.system.service.ITDaRealtimeAllService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实时状态Service业务层处理
 *
 * @author Liu
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class TDaRealtimeAllServiceImpl implements ITDaRealtimeAllService {

    private final TDaRealtimeAllMapper baseMapper;

    /**
     * 查询实时状态
     */
    @Override
    public TDaRealtimeAllVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询实时状态列表
     */
    @Override
    public TableDataInfo<TDaRealtimeAllVo> queryPageList(TDaRealtimeAllBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TDaRealtimeAll> lqw = buildQueryWrapper(bo);
        Page<TDaRealtimeAllVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询实时状态列表
     */
    @Override
    public List<TDaRealtimeAllVo> queryList(TDaRealtimeAllBo bo) {
        LambdaQueryWrapper<TDaRealtimeAll> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TDaRealtimeAll> buildQueryWrapper(TDaRealtimeAllBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TDaRealtimeAll> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEquipCode()), TDaRealtimeAll::getEquipCode, bo.getEquipCode());
        lqw.eq(StringUtils.isNotBlank(bo.getModelCode()), TDaRealtimeAll::getModelCode, bo.getModelCode());
        lqw.eq(StringUtils.isNotBlank(bo.getMouldCode()), TDaRealtimeAll::getMouldCode, bo.getMouldCode());
        lqw.eq(bo.getStatusCode() != null, TDaRealtimeAll::getStatusCode, bo.getStatusCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLiquidLevel()), TDaRealtimeAll::getLiquidLevel, bo.getLiquidLevel());
        lqw.eq(bo.getUpdateTimestamp() != null, TDaRealtimeAll::getUpdateTimestamp, bo.getUpdateTimestamp());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve1()), TDaRealtimeAll::getReserve1, bo.getReserve1());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve2()), TDaRealtimeAll::getReserve2, bo.getReserve2());
        lqw.eq(StringUtils.isNotBlank(bo.getProductionBeat()), TDaRealtimeAll::getProductionBeat, bo.getProductionBeat());
        lqw.like(StringUtils.isNotBlank(bo.getShortName()), TDaRealtimeAll::getShortName, bo.getShortName());
        lqw.eq(StringUtils.isNotBlank(bo.getLineCode()), TDaRealtimeAll::getLineCode, bo.getLineCode());
        lqw.eq(bo.getPlantNO() != null, TDaRealtimeAll::getPlantNO, bo.getPlantNO());
        lqw.eq(StringUtils.isNotBlank(bo.getDataFlag()), TDaRealtimeAll::getDataFlag, bo.getDataFlag());
        return lqw;
    }

    /**
     * 新增实时状态
     */
    @Override
    public Boolean insertByBo(TDaRealtimeAllBo bo) {
        TDaRealtimeAll add = MapstructUtils.convert(bo, TDaRealtimeAll.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改实时状态
     */
    @Override
    public Boolean updateByBo(TDaRealtimeAllBo bo) {
        TDaRealtimeAll update = MapstructUtils.convert(bo, TDaRealtimeAll.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TDaRealtimeAll entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除实时状态
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
